package cfca.paperless.client.servlet._5_query._5_01_QueryProofInfoList;

import cfca.paperless.client.bean.QueryBean;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StringUtil;

/**
 * @Description
 * @Author dell
 * @Date 2016-5-13
 * @CodeReviewer TODO
 */
public class QueryProofInfoListTest01 {

    public static void main(String[] args) throws Exception {
        try {
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.queryUrl, 10000, 60000);// 无纸化辅助接口的访问URL

            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;

            QueryBean queryBean = new QueryBean("20170201171642", "20170228171642", "B001", "", "1");
            String queryXml = PaperlessUtil.buildQueryXml(queryBean);
            System.out.println(queryXml);
            String resultXml = clientBean.queryProofInfoList(queryXml, "gy001");

            String code = StringUtil.getNodeText(resultXml, "Code");
            if (!code.equals("200")) {
                System.out.println("NG:queryProofInfoList");
                System.out.println(resultXml);
            } else {
                
                String totalPage = StringUtil.getNodeText(resultXml, "TotalPage");
                String totalCount = StringUtil.getNodeText(resultXml, "TotalCount");
                int totalCountInt = Integer.valueOf(totalCount);
                if (totalCountInt > 0) {
                    Object[] proofInfoObjArr = StringUtil.getXmlFieldArr(resultXml, "ProofInfo");
                    for (Object obj : proofInfoObjArr) {
                        String bizSerialNo = StringUtil.getNodeText(obj.toString(), "BizSerialNo");
                        clientBean = new PaperlessClient(PaperlessConfig.queryUrl, 10000, 60000);

                        byte[] pdf = clientBean.queryProofFile(bizSerialNo);
                        String errorRsString = PaperlessUtil.isError(pdf);
                        if ("".equals(errorRsString)) {
                            IoUtil.write("./TestData/output/queryProofFile-" + bizSerialNo + ".pdf", pdf);
                        } else {
                            System.out.println(new String(pdf));
                        }
                    }
                    System.out.println(proofInfoObjArr.length);
                }
                System.out.println("totalPage:" + totalPage);
                System.out.println("totalCount:" + totalCount);
                System.out.println(resultXml);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
